package com.z.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.z.sys.model.Permission;
import com.z.sys.mapper.PermissionMapper;
import com.z.sys.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.z.sys.vo.DTree;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxy
 * @since 2020-10-14
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public List<DTree> getTree(String roleId) {
        List<Permission> permissions = permissionMapper.selectList(new QueryWrapper<Permission>().orderByAsc("parent_Id", "sort"));
        List<Integer> checkedIds = permissionMapper.selectCheckedId(roleId);
        Map<Integer, List<Permission>> pMap = new HashMap<>();
        for(Permission p : permissions){
            if(pMap.get(p.getParentId()) == null){
                pMap.put(p.getParentId(),new ArrayList<Permission>());
            }
            pMap.get(p.getParentId()).add(p);
        }
        List<DTree> pTree = new ArrayList<>();
        for(Permission p : permissions){
            if(p.getParentId()==-1){
                DTree pt = new DTree(p.getId().toString(), "-1", p.getName());

                pt.setChildren(getChildren(p.getId(), pMap, checkedIds));
                pTree.add(pt);
            }
        }
        return pTree;
    }

    @Override
    public List<DTree> selectTree() {
        List<Permission> permissions = permissionMapper.selectList(new QueryWrapper<Permission>().orderByAsc("parent_Id", "sort"));
        Map<Integer, List<Permission>> pMap = new HashMap<>();
        for(Permission p : permissions){
            if(pMap.get(p.getParentId()) == null){
                pMap.put(p.getParentId(),new ArrayList<Permission>());
            }
            pMap.get(p.getParentId()).add(p);
        }
        List<DTree> pTree = new ArrayList<>();
        for(Permission p : permissions){
            if(p.getParentId()==-1){
                DTree pt = new DTree(p.getId().toString(), "-1", p.getName());
                pt.setChildren(getChildren(p.getId(),pMap));
                pTree.add(pt);
            }
        }
        return pTree;
    }

    @Override
    public List<DTree> getMenu(String userId) {
        List<Permission> permissions = permissionMapper.selectListByUseId(userId);
        Map<Integer, List<Permission>> pMap = new HashMap<>();
        for(Permission p : permissions){
            if(pMap.get(p.getParentId()) == null){
                pMap.put(p.getParentId(),new ArrayList<Permission>());
            }
            pMap.get(p.getParentId()).add(p);
        }
        List<DTree> pTree = new ArrayList<>();
        for(Permission p : permissions){
            if(p.getParentId()==0){
                DTree pt = new DTree(p.getId().toString(), "0", p.getName());
                pt.setChildren(getChildren(p.getId(),pMap));
                pTree.add(pt);
            }
        }
        return pTree;
    }

    private List<DTree> getChildren(Integer parantId, Map<Integer, List<Permission>> pMap) {

        List<DTree> permissionTrees = new ArrayList<>();
        List<Permission> permissions = pMap.get(parantId);
        if(!CollectionUtils.isEmpty(permissions)){
            for(Permission p : permissions){
                DTree pt = new DTree(p.getId().toString(), parantId.toString(),p.getName(), p.getHref());
                pt.setChildren(getChildren(p.getId(),pMap));
                permissionTrees.add(pt);
            }
        }

        return permissionTrees;
    }
    private List<DTree> getChildren(Integer parantId, Map<Integer, List<Permission>> pMap, List<Integer> checkedIds) {

        List<DTree> permissionTrees = new ArrayList<>();
        List<Permission> permissions = pMap.get(parantId);
        if(!CollectionUtils.isEmpty(permissions)){
            for(Permission p : permissions){
                DTree pt = new DTree(p.getId().toString(), parantId.toString(),p.getName(), p.getHref());

                pt.setChildren(getChildren(p.getId(), pMap, checkedIds));
                if(CollectionUtils.isEmpty(pt.getChildren()) && checkedIds.contains(p.getId())){
                    pt.setChecked(true);
                }
                permissionTrees.add(pt);
            }
        }

        return permissionTrees;
    }
}
